<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>人脸活体检测系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            color: #333;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }

        .header {
            text-align: center;
            margin-bottom: 40px;
            color: white;
        }

        .header h1 {
            font-size: 2.5rem;
            margin-bottom: 10px;
            text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
        }

        .header p {
            font-size: 1.1rem;
            opacity: 0.9;
        }

        .nav-bar {
            background: white;
            border-radius: 15px;
            padding: 15px 20px;
            margin-bottom: 30px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .nav-brand {
            font-size: 1.5rem;
            font-weight: bold;
            color: #333;
        }

        .nav-links {
            display: flex;
            gap: 20px;
        }

        .nav-link {
            color: #667eea;
            text-decoration: none;
            font-weight: 600;
            padding: 8px 16px;
            border-radius: 8px;
            transition: all 0.3s ease;
        }

        .nav-link:hover {
            background: #f0f0f0;
        }

        .main-content {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 30px;
            margin-bottom: 30px;
        }

        .video-section {
            background: white;
            border-radius: 15px;
            padding: 20px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
        }

        .video-container {
            position: relative;
            border-radius: 10px;
            overflow: hidden;
            background: #f0f0f0;
            min-height: 300px;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .video-container img {
            max-width: 100%;
            height: auto;
            border-radius: 10px;
        }

        .controls {
            margin-top: 20px;
            display: flex;
            gap: 15px;
            flex-wrap: wrap;
        }

        .btn {
            padding: 12px 24px;
            border: none;
            border-radius: 8px;
            font-size: 1rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            text-transform: uppercase;
            letter-spacing: 1px;
        }

        .btn-primary {
            background: linear-gradient(45deg, #667eea, #764ba2);
            color: white;
        }

        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
        }

        .btn-secondary {
            background: linear-gradient(45deg, #f093fb, #f5576c);
            color: white;
        }

        .btn-secondary:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(240, 147, 251, 0.4);
        }

        .btn-info {
            background: linear-gradient(45deg, #4facfe, #00f2fe);
            color: white;
        }

        .btn-info:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(79, 172, 254, 0.4);
        }

        .result-section {
            background: white;
            border-radius: 15px;
            padding: 20px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
        }

        .result-header {
            text-align: center;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 2px solid #f0f0f0;
        }

        .result-header h3 {
            color: #333;
            font-size: 1.5rem;
            margin-bottom: 5px;
        }

        .result-header p {
            color: #666;
            font-size: 0.9rem;
        }

        .result-content {
            display: grid;
            gap: 15px;
        }

        .result-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 12px 15px;
            background: #f8f9fa;
            border-radius: 8px;
            border-left: 4px solid #667eea;
        }

        .result-label {
            font-weight: 600;
            color: #555;
        }

        .result-value {
            font-weight: 500;
            color: #333;
        }

        .liveness-status {
            padding: 8px 16px;
            border-radius: 20px;
            font-weight: 600;
            text-align: center;
            margin-top: 10px;
        }

        .liveness-pass {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .liveness-fail {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .detection-image {
            margin-top: 20px;
            text-align: center;
        }

        .detection-image img {
            max-width: 100%;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
        }

        .loading {
            display: none;
            text-align: center;
            padding: 20px;
        }

        .spinner {
            border: 4px solid #f3f3f3;
            border-top: 4px solid #667eea;
            border-radius: 50%;
            width: 40px;
            height: 40px;
            animation: spin 1s linear infinite;
            margin: 0 auto 10px;
        }

        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }

        .features {
            background: white;
            border-radius: 15px;
            padding: 30px;
            margin-bottom: 30px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
        }

        .features h2 {
            text-align: center;
            margin-bottom: 30px;
            color: #333;
            font-size: 2rem;
        }

        .features-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
        }

        .feature-item {
            text-align: center;
            padding: 20px;
            background: #f8f9fa;
            border-radius: 10px;
            border-left: 4px solid #667eea;
        }

        .feature-item h3 {
            color: #333;
            margin-bottom: 10px;
        }

        .feature-item p {
            color: #666;
            line-height: 1.6;
        }

        @media (max-width: 768px) {
            .main-content {
                grid-template-columns: 1fr;
            }
            
            .controls {
                justify-content: center;
            }
            
            .header h1 {
                font-size: 2rem;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>人脸活体检测系统</h1>
            <p>基于AI的智能人脸识别与活体检测</p>
        </div>

        <div class="nav-bar">
            <div class="nav-brand">人脸活体检测系统</div>
            <div class="nav-links">
                <a href="{{ url_for('index') }}" class="nav-link">活体检测</a>
                <a href="{{ url_for('upload_page') }}" class="nav-link">Deepfake检测</a>
                <a href="{{ url_for('logout') }}" class="nav-link">登出</a>
            </div>
        </div>

        <div class="features">
            <h2>系统功能</h2>
            <div class="features-grid">
                <div class="feature-item">
                    <h3>🎯 人脸检测</h3>
                    <p>精准识别图像中的人脸位置，支持多人脸检测</p>
                </div>
                <div class="feature-item">
                    <h3>🔍 活体检测</h3>
                    <p>防止照片攻击，确保检测的是真实人脸</p>
                </div>
                <div class="feature-item">
                    <h3>👤 属性分析</h3>
                    <p>分析年龄、性别、情绪等人脸属性</p>
                </div>
                <div class="feature-item">
                    <h3>📊 可视化结果</h3>
                    <p>直观展示检测结果，支持结果图片保存</p>
                </div>
            </div>
        </div>

        <div class="main-content">
            <div class="video-section">
                <h3 style="margin-bottom: 15px; color: #333;">实时摄像头</h3>
                <div class="video-container">
                    <img id="video" src="" alt="摄像头画面">
                </div>
                <div class="controls">
                    <button id="start" class="btn btn-primary">开启摄像头</button>
                    <button id="stop" class="btn btn-secondary">关闭摄像头</button>
                    <button id="detect" class="btn btn-info">开始检测</button>
                </div>
                <div class="loading" id="loading">
                    <div class="spinner"></div>
                    <p>正在进行活体检测...</p>
                </div>
            </div>

            <div class="result-section">
                <div class="result-header">
                    <h3>检测结果</h3>
                    <p>点击"开始检测"按钮进行人脸活体检测</p>
                </div>
                <div class="result-content" id="result-content">
                    <div class="result-item">
                        <span class="result-label">检测状态</span>
                        <span class="result-value">等待检测</span>
                    </div>
                </div>
                <div class="detection-image" id="detection-image" style="display: none;">
                    <h4 style="margin-bottom: 10px;">检测结果图片</h4>
                    <img id="result-img" src="" alt="检测结果">
                </div>
            </div>
        </div>
    </div>

    <script>
        document.getElementById('start').addEventListener('click', () => {
            document.getElementById('video').src = '/video_feed';
        });

        document.getElementById('stop').addEventListener('click', () => {
            document.getElementById('video').src = '';
        });

        document.getElementById('detect').addEventListener('click', () => {
            const loading = document.getElementById('loading');
            const resultContent = document.getElementById('result-content');
            
            // 显示加载状态
            loading.style.display = 'block';
            resultContent.innerHTML = '<div class="result-item"><span class="result-label">检测状态</span><span class="result-value">检测中...</span></div>';

            fetch('/recognize', { 
                method: 'POST', 
                body: JSON.stringify({}), 
                headers: { 'Content-Type': 'application/json' } 
            })
            .then(response => response.json())
            .then(data => {
                loading.style.display = 'none';
                
                if (data.error) {
                    resultContent.innerHTML = `
                        <div class="result-item">
                            <span class="result-label">错误信息</span>
                            <span class="result-value" style="color: #dc3545;">${data.error}</span>
                        </div>
                    `;
                    return;
                }

                // 解析检测结果
                const faceData = data.face_data || {};
                const livenessData = data.liveness_data || {};
                
                let resultHTML = '';
                
                // 活体检测结果
                const livenessResult = livenessData.result || {};
                const faceLiveness = livenessResult.face_liveness || '未知';
                const livenessClass = faceLiveness > 0.5 ? 'liveness-pass' : 'liveness-fail';
                const livenessText = faceLiveness > 0.5 ? '通过' : '未通过';
                
                resultHTML += `
                    <div class="result-item">
                        <span class="result-label">活体检测</span>
                        <span class="result-value">${faceLiveness}</span>
                    </div>
                    <div class="liveness-status ${livenessClass}">
                        活体检测: ${livenessText}
                    </div>
                `;
                
                // 人脸属性
                const faceList = faceData.result?.face_list || [];
                if (faceList.length > 0) {
                    const face = faceList[0];
                    resultHTML += `
                        <div class="result-item">
                            <span class="result-label">年龄</span>
                            <span class="result-value">${face.age || '未知'}</span>
                        </div>
                        <div class="result-item">
                            <span class="result-label">性别</span>
                            <span class="result-value">${face.gender?.type || '未知'}</span>
                        </div>
                        <div class="result-item">
                            <span class="result-label">情绪</span>
                            <span class="result-value">${face.emotion?.type || '未知'}</span>
                        </div>
                    `;
                } else {
                    resultHTML += `
                        <div class="result-item">
                            <span class="result-label">检测结果</span>
                            <span class="result-value" style="color: #dc3545;">未检测到人脸</span>
                        </div>
                    `;
                }
                
                resultContent.innerHTML = resultHTML;
                
                // 显示检测结果图片
                if (data.output_image) {
                    const detectionImage = document.getElementById('detection-image');
                    const resultImg = document.getElementById('result-img');
                    resultImg.src = data.output_image + '?t=' + new Date().getTime();
                    detectionImage.style.display = 'block';
                }
            })
            .catch(error => {
                loading.style.display = 'none';
                console.error('Error:', error);
                document.getElementById('result-content').innerHTML = `
                    <div class="result-item">
                        <span class="result-label">错误信息</span>
                        <span class="result-value" style="color: #dc3545;">网络错误，请重试</span>
                    </div>
                `;
            });
        });
    </script>
</body>
</html>